<?php
	class Admincp_Model_News extends Zend_Db_Table_Abstract
	{
		protected $_name = 'news';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem()
        {
            $data = array(
                'id',
                'title',
                'image',
                'create_date',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
            return $sql;
        }
        
        public function getItem($id)
        {
            $data = array(
                'title',
                'image',
                'description',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getImageById($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('image'))
                            ->where('id = ?', $id);
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
        
        public function saveItem($data, $option = null)
        {
            if($option == 'create') {
                $this->_db->insert($this->_name, $data);
            }
            else if($option == 'update') {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update($this->_name, $data, $where);
            }
        }
        
        public function deleteItem($id)
		{
		    $id = (int) $id;
            $image = $this->getImageById($id);
            if(is_file(PUBLIC_PATH . '/files/news/' . $image)) {
                @unlink(PUBLIC_PATH . '/files/news/' . $image);
            }
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
		    $this->_db->delete($this->_name, $where);
		}
		
		public function deleteMultiItem($listId)
		{
		    foreach($listId as $value) {
		        $this->deleteItem($value);
		    }
		}
        
        public function searchItem($data)
		{
            $common = new Admincp_Model_Common();
		    $news = array(
		        'id',
                'title',
                'image',
                'create_date',
                'status',
		    );
		    $sql = $this->_db->select()
		                    ->from($this->_name, $news);
		    if($data['title'] != '') {
		        $sql = $sql->where('url LIKE ?', $data['title']);
		    }
		    if($data['start_date'] !== '') {
		        $sql = $sql->where('create_date >= ?', $common->formatDate($data['start_date']));
		    }
		    if($data['end_date'] !== '') {
		        $sql = $sql->where('create_date <= ?', $common->formatDate($data['end_date']));
		    }
		    if($data['status'] != 0) {
		        $sql = $sql->where('status = ?', $data['status']);
		    }
		    $sql = $sql->order('title ASC');
		    return $sql;
		}
    }
